<?php
 Session_Start () ;
 Include ("../../include/garennes.cfg") ;
 Include ("../../include/init.php") ;
 Include ("../../include/module_trombinoscope.cfg") ;
 Include ("../../include/module_trombinoscope.php") ;
 Include ("../include/config.inc") ;
 verif_droits ($droit_courant, $_SESSION ["code"]) ;
 
 Include ("../../include/var_temp.inc") ;
 
 $requete = var_courante ("requete", 0, null) ;
 
 If (IsSet ($requete))
  {
   $type = var_courante ("type", 0, 0) ;
   Switch ($requete)
    {
     // Insertion d'une sequence de traitement
     Case 0 : $ordre = var_courante ("ordre", 0, 0) ;
              $ordre_max = var_courante ("ordre_max", 0, 0) ;
	      $numero = var_courante ("numero", 0, 0) ;
	      $id_tromb_type_sequence = var_courante ("id_tromb_type_sequence", 0, 0) ;
	      $param = var_courante ("param", 0, "") ;
	      $num_ordre = var_courante ("num_ordre", 0, 0) ;
              If ($ordre == -1)
               {
	        // Verification de l'ordre
		$res_max = SQL_commande ("SELECT MAX(ordre) FROM tromb_sequences_retaillage WHERE type='".$type."' AND numero='".$numero."'", $id_sql) ;
                $max = SQL_ligne_suivante ($res_max) ;
		If (($max [0] != "NULL") And ($max [0] == $ordre_max)) { $num_ordre = $ordre_max+1 ; }
		ElseIf ($max [0] != "NULL") { $num_ordre = $max [0]+1 ; }
		Else { $num_ordre = 1 ; }
                $ok = SQL_commande ("INSERT INTO tromb_sequences_retaillage (id_tromb_type_sequence,numero,type,param,ordre)
                                     VALUES ('".$id_tromb_type_sequence."','".$numero."','".$type."','".$param."','".$num_ordre."')", $id_sql) ;
               }
	      Else
	       {
	        For ($i = $ordre+1 ; $i <= $ordre_max ; $i++)
		 {
		  $ok = SQL_commande ("UPDATE tromb_sequences_retaillage SET ordre='".($i+1)."' WHERE ordre='".$i."' AND numero='".$numero."' AND type='".$type."' LIMIT 1", $id_sql) ;
		 }
		$ok = SQL_commande ("INSERT INTO tromb_sequences_retaillage (id_tromb_type_sequence,numero,type,param,ordre)
                                     VALUES ('".$id_tromb_type_sequence."','".$numero."','".$type."','".$param."','".($ordre+1)."')", $id_sql) ;
	       }
	      $requete = 0 ;
              Break ;
     // Effacement d'une sequence de traitement
     Case 1 : $id_tromb_sequence_retaillage = var_courante ("id_tromb_sequence_retaillage", 0, 0) ;
              $ordre_max = var_courante ("ordre_max", 0, 0) ;
              $res = SQL_commande ("SELECT ordre,numero,type FROM tromb_sequences_retaillage WHERE id_tromb_sequence_retaillage='".$id_tromb_sequence_retaillage."' LIMIT 1", $id_sql) ;
	      $ligne = SQL_ligne_suivante ($res) ;
	      For ($i = $ligne [0] ; $i < $ordre_max ; $i++)
	       {
	        $ok = SQL_commande ("UPDATE tromb_sequences_retaillage SET ordre='".$i."' WHERE ordre='".($i+1)."' AND numero='".$ligne [1]."' AND type='".$ligne [2]."' LIMIT 1", $id_sql) ;
	       }
              $ok = SQL_commande ("DELETE FROM tromb_sequences_retaillage WHERE id_tromb_sequence_retaillage='".$id_tromb_sequence_retaillage."' LIMIT 1", $id_sql) ;
	      $requete = 0 ;
	      Break ;
     // Debut de modification
     Case 2 : $id_tromb_sequence_retaillage = var_courante ("id_tromb_sequence_retaillage", 0, 0) ;
              $resultat = SQL_commande ("SELECT s.id_tromb_sequence_retaillage,t.id_tromb_type_sequence,t.nom,s.param,s.ordre,s.type,s.numero FROM tromb_sequences_retaillage s,tromb_types_sequences t WHERE s.id_tromb_type_sequence=t.id_tromb_type_sequence AND s.id_tromb_sequence_retaillage='".$id_tromb_sequence_retaillage."' LIMIT 1", $id_sql) ;
              UnSet ($defaut_prog) ;
              $defaut_prog = SQL_tableau_associatif ($resultat, "") ;
	      $requete = 3 ;
              Break ;
     // Fin de modification
     Case 3 : $id_tromb_type_sequence = var_courante ("id_tromb_type_sequence", 0, 0) ;
              $id_tromb_sequence_retaillage = var_courante ("id_tromb_sequence_retaillage", 0, 0) ;
              $param = var_courante ("param", 0, "") ;
              $ok = SQL_commande ("UPDATE tromb_sequences_retaillage SET id_tromb_type_sequence='".$id_tromb_type_sequence."',param='".$param."'
 			            WHERE id_tromb_sequence_retaillage='".$id_tromb_sequence_retaillage."' LIMIT 1", $id_sql) ;
	      $requete = 0 ;
              Break ;
    }
  }
 Else
  { $requete = 0 ; }
 If ($requete == 0)
  {
   $defaut_prog ["id_tromb_type_sequence"] = 0 ;
   $defaut_prog ["numero"] = 0 ;
   $defaut_prog ["ordre"] = 0 ;
  }
 
 $resultats = SQL_commande ("SELECT * FROM menus WHERE numero='".$droit_courant."' AND ordre='0' LIMIT 1", $id_sql) ;
 $l = SQL_tableau_associatif ($resultats, "") ; 
 Echo "<HTML><HEAD>" ;
 entete_html ("Garennes", "travail", 1) ;
 Echo styles ("none", "none", "none", $l ["lien"], "none") ;
 Include ("./prog_traitement.js") ;
 Echo "</HEAD>" ;
 
 Echo "<BODY BACKGROUND=\"../images/fond.png\" TEXT=\"#000000\" onContextMenu=\"return false\">" ;
 Echo "<CENTER><FONT COLOR=\"#0000FF\"><B>D&eacute;finition&nbsp;des&nbsp;s&eacute;quences&nbsp;de&nbsp;retaillage&nbsp;des&nbsp;photos.</B></FONT></CENTER>" ;
 
 // Determine le type de programme utilise (interne ou externe)
 $type_prog = retrouve_variable ("application_traitement") ;
 If ($type_prog == "interne") { $type = 0 ; } Else { $type = 1  ;}
 
 If ($requete == 0)
  {
   $res = SQL_commande ("SELECT * FROM tromb_progs_retaille WHERE type='".$type."' ORDER BY nom,numero", $id_sql) ;
   If (SQL_nbr_lignes ($res))
    {
     Echo "<CENTER>&nbsp;Programmes&nbsp;d&eacute;j&agrave;&nbsp;d&eacute;finis&nbsp;pour&nbsp;" ;
     If ($type == 0) { Echo "la&nbsp;biblioth&egrave;que&nbsp;<B>GD</B>" ; } Else { Echo "le&nbsp;programme&nbsp;<B>".$nom_prog_externe."</B>" ; }
     Echo ".&nbsp;</CENTER>" ;
     For ($i = 0 ; $i < SQL_nbr_lignes ($res) ; $i++)
      {
       $ligne = SQL_tableau_associatif ($res, "") ;
       $progs = SQL_commande ("SELECT s.id_tromb_sequence_retaillage,t.nom,t.option,s.param,s.ordre,s.numero FROM tromb_sequences_retaillage s,tromb_types_sequences t WHERE s.numero='".$ligne ["numero"]."' AND s.type='".$ligne ["type"]."' AND s.id_tromb_type_sequence=t.id_tromb_type_sequence ORDER BY s.ordre", $id_sql) ;
       Echo "<TABLE ALIGN=\"CENTER\" BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"0\" BGCOLOR=\"".$l ["couleur_fond"]."\" STYLE=\"border:1px solid #000000;border-collapse:collapse\">" ;
       Echo "<TR BGCOLOR=\"".$l ["lien"]."\"><TD COLSPAN=\"4\" ALIGN=\"CENTER\">&nbsp;".Str_Replace (" ", "&nbsp;", $ligne ["nom"])."&nbsp;</TD></TR>" ;
       If (SQL_nbr_lignes ($progs) > 0)
        {
	 Echo "<TR BGCOLOR=\"".$l ["lien"]."\">" ;
         Echo "<TD></TD>" ;
         Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\"><B>Num&eacute;ro</B></FONT>&nbsp;</TD>" ;
         Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\"><B>Fonction</B></FONT>&nbsp;</TD>" ;
         //Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\"><B>Option</B></FONT>&nbsp;</TD>" ;
         Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\"><B>Param&egrave;tre</B></FONT>&nbsp;</TD>" ;
         Echo "</TR>" ;
	 $nbr_progs = SQL_nbr_lignes ($progs) ;
	 For ($j = 0 ; $j < $nbr_progs ; $j++)
          {
	   $pg = SQL_tableau_associatif ($progs, "") ;
           Echo "<TR" ;
           If ($j%2 == 0) { Echo " BGCOLOR=\"".$l ["nom"]."\"" ; }
           Echo ">" ;
           Echo "<TD><TABLE WIDTH=\"100%\" BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\"><TR>" ;
           
           Echo "<FORM ACTION=\"".$_SERVER ["PHP_SELF"]."\" METHOD=\"POST\">" ;
           Echo "<TD>" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"code\" VALUE=\"".$_SESSION ["code"]."\">" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"id_tromb_sequence_retaillage\" VALUE=\"".$pg ["id_tromb_sequence_retaillage"]."\">" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"requete\" VALUE=\"2\">" ;
           Echo "<INPUT TYPE=\"IMAGE\" SRC=\"../images/edition.png\" TITLE=\"&Eacute;diter cette s&eacute;quence\" BORDER=\"0\">" ;
           Echo "</TD>" ;
           Echo "</FORM>" ;
           
	   $res_max = SQL_commande ("SELECT MAX(ordre) FROM tromb_sequences_retaillage WHERE type='".$type."' AND numero='".$pg ["numero"]."'", $id_sql) ;
           $max = SQL_ligne_suivante ($res_max) ;
           Echo "<FORM ACTION=\"".$_SERVER ["PHP_SELF"]."\" METHOD=\"POST\" onSubmit=\"return valide('".$pg ["ordre"]."','".Str_Replace ("'", "\'", $pg ["nom"])."')\">" ;
           Echo "<TD>" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"code\" VALUE=\"".$_SESSION ["code"]."\">" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"id_tromb_sequence_retaillage\" VALUE=\"".$pg ["id_tromb_sequence_retaillage"]."\">" ;
	   Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"ordre_max\" VALUE=\"".$max [0]."\">" ;
           Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"requete\" VALUE=\"1\">" ;
           Echo "<INPUT TYPE=\"IMAGE\" SRC=\"../images/effacer.png\" TITLE=\"Supprimer cette s&eacute;quence\" BORDER=\"0\">" ;
           Echo "</TD></FORM>" ;
           
           Echo "</TR></TABLE></TD>" ;
	   
	   Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\">".$pg ["ordre"]."</FONT>&nbsp;</TD>" ;
	   Echo "<TD>&nbsp;<FONT SIZE=\"".$l ["titre"]."\">".$pg ["nom"]."</FONT>&nbsp;</TD>" ;
	   //Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\">".$pg ["option"]."</FONT>&nbsp;</TD>" ;
	   Echo "<TD ALIGN=\"CENTER\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\">".$pg ["param"]."</FONT>&nbsp;</TD>" ;
	   Echo "</TR>" ;
	  }
         Echo "</TABLE><BR>" ;
	}
       Else
        {
	 // Aucun parametres pour ce programme
	 Echo "<TR BGCOLOR=\"".$l ["lien"]."\"><TD COLSPAN=\"5\" ALIGN=\"CENTER\">&nbsp;Aucun&nbsp;param&egrave;tres&nbsp;pour&nbsp;ce&nbsp;programme.&nbsp;</TD></TR>" ;
	 Echo "</TABLE><BR>" ;
	}	
      }
    }
  }
 
 Echo "<FORM ACTION=\"".$_SERVER ["PHP_SELF"]."\" METHOD=\"POST\">" ;
 Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"requete\" VALUE=\"".$requete."\">" ;
 Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"id_tromb_sequence_retaillage\" VALUE=\"".$defaut_prog ["id_tromb_sequence_retaillage"]."\">" ;
 Echo "<INPUT TYPE=\"HIDDEN\" NAME=\"type\" VALUE=\"".$type."\">" ;
 Echo "<TABLE ALIGN=\"CENTER\" BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"0\" BGCOLOR=\"".$l ["couleur_fond"]."\" STYLE=\"border:1px solid #000000;border-collapse:collapse\">" ;
 If ($requete == 0) { Echo "<TR BGCOLOR=\"".$l ["lien"]."\"><TD ALIGN=\"CENTER\"><FONT SIZE=\"".$l ["titre"]."\">&nbsp;<B>Ajouter&nbsp;une&nbsp;s&eacute;quence&nbsp;de&nbsp;retaillage</B>&nbsp;</FONT></TD></TR>" ; }
 Else { Echo "<TR BGCOLOR=\"".$l ["lien"]."\"><TD ALIGN=\"CENTER\"><FONT SIZE=\"".$l ["titre"]."\">&nbsp;<B><BLINK>Modifier</BLINK>&nbsp;une&nbsp;s&eacute;quence&nbsp;de&nbsp;retaillage</B>&nbsp;</FONT></TD></TR>" ; }
 Echo "<TR><TD><TABLE WIDTH=\"100%\" CELLSPACING=\"0\" CELLPADDING=\"2\">" ;
 Echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>" ;

 // Choix du Programme
 Echo "<TR BGCOLOR=\"".$l ["nom"]."\">" ;
 Echo "<TD ALIGN=\"RIGHT\"><FONT SIZE=\"".$l ["titre"]."\"><B>&nbsp;Programme&nbsp;</B></FONT></TD>" ;
 If ($requete == 0)
  {
   Echo "<TD><SELECT NAME=\"numero\">" ;
   $res = SQL_commande ("SELECT numero,nom FROM tromb_progs_retaille WHERE type='".$type."' ORDER BY nom", $id_sql) ;
   For ($i = 0 ; $i < SQL_nbr_lignes ($res) ; $i++)
    {
     $ligne = SQL_ligne_suivante ($res) ;
     Echo "<OPTION VALUE=\"".$ligne [0]."\"" ; If ($defaut_prog ["numero"] == $ligne [0]) { Echo " SELECTED" ; } Echo ">".$ligne [1]."</OPTION>" ;
    }
   Echo "</SELECT>&nbsp;</TD>" ;
  }
 Else
  {
   $res = SQL_commande ("SELECT nom FROM tromb_progs_retaille WHERE type='".$type."' AND numero='".$defaut_prog ["numero"]."' LIMIT 1", $id_sql) ;
   $ligne = SQL_ligne_suivante ($res) ;
   Echo "<TD><FONT SIZE=\"".$l ["titre"]."\">".$ligne [0]."</FONT>&nbsp;</TD>" ;
  }
 Echo "</TR>" ;

 // Choix du numero de la sequence
 Echo "<TR><TD ALIGN=\"RIGHT\">&nbsp;<FONT SIZE=\"".$l ["titre"]."\"><B>Num&eacute;ro&nbsp;de&nbsp;s&eacute;quence</B></FONT>&nbsp;</TD>" ;
 If ($requete == 0)
  {
   $res = SQL_commande ("SELECT MAX(ordre) FROM tromb_sequences_retaillage WHERE type='".$type."'", $id_sql) ;
   $max = SQL_ligne_suivante ($res) ;
   If ($max [0] > 0)
    {
     Echo "<TD><SELECT NAME=\"ordre\">" ;
     Echo "<OPTION VALUE=\"-1\">Apr&egrave;s la derni&egrave;re s&eacute;quence</OPTION>" ;
     For ($i = 0 ; $i < $max [0] ; $i++)
      {
       Echo "<OPTION VALUE=\"".$i."\">" ;
       If ($i > 0) { Echo "Entre la s&eacute;quence ".$i." et ".($i+1) ; } Else { Echo "Avant la premi&egrave;re s&eacute;quence " ; }
       Echo "</OPTION>" ;
      }
     Echo "</SELECT>&nbsp;<INPUT TYPE=\"HIDDEN\" NAME=\"ordre_max\" VALUE=\"".$max [0]."\"></TD>" ;
    }
   Else
    {
     Echo "<TD><INPUT TYPE=\"HIDDEN\" NAME=\"ordre\" VALUE=\"-1\"><FONT SIZE=\"".$l ["titre"]."\">Nouvelle s&eacute;quence</FONT>" ;
     Echo "&nbsp;<INPUT TYPE=\"HIDDEN\" NAME=\"ordre_max\" VALUE=\"0\"></TD>" ;
    }
  }
 Else { Echo "<TD><FONT SIZE=\"".$l ["titre"]."\">".$defaut_prog ["ordre"]."</FONT>&nbsp;</TD>" ; }
 Echo "</TR>" ;

 // Choix de l'option
 Echo "<TR>" ;
 Echo "<TD ALIGN=\"RIGHT\"><FONT SIZE=\"".$l ["titre"]."\"><B>&nbsp;Option&nbsp;</B></FONT></TD>" ;
 Echo "<TD><SELECT NAME=\"id_tromb_type_sequence\">" ;
 $res = SQL_commande ("SELECT * FROM tromb_types_sequences WHERE type='".$type."' ORDER BY nom", $id_sql) ;
 For ($i = 0 ; $i < SQL_nbr_lignes ($res) ; $i++)
  {
   $ligne = SQL_tableau_associatif ($res, "") ;
   Echo "<OPTION VALUE=\"".$ligne ["id_tromb_type_sequence"]."\"" ;
   If ($defaut_prog ["id_tromb_type_sequence"] == $ligne ["id_tromb_type_sequence"]) { Echo " SELECTED" ; }
   Echo ">".$ligne ["nom"]."</OPTION>" ;
  }
 Echo "</SELECT>&nbsp;</TD></TR>" ;

 // Choix du parametre
 Echo "<TR BGCOLOR=\"".$l ["nom"]."\">" ;
 Echo "<TD ALIGN=\"RIGHT\"><FONT SIZE=\"".$l ["titre"]."\"><B>&nbsp;Param&egrave;tres&nbsp;</B></FONT></TD>" ;
 Echo "<TD><FONT SIZE=\"".$l ["titre"]."\"><INPUT TYPE=\"TEXT\" NAME=\"param\" VALUE=\"".$defaut_prog ["param"]."\" SIZE=\"35\" MAXLENGTH=\"200\">&nbsp;</FONT></TD>" ;
 Echo "</TR>" ;
 
 Echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>" ;
 Echo "<TR><TD COLSPAN=\"2\" ALIGN=\"CENTER\"><INPUT TYPE=\"IMAGE\" SRC=\"../images/valider.png\" ALT=\"Ajouter/Modifier un jour de passage\" VALUE=\"VaLiDeR\" BORDER=\"0\"></TD></TR>" ;
 Echo "</TABLE></TD></TR></TABLE>" ;
 Echo "</FORM>" ;
 If ($requete != 0)
  {
   Echo "<BR><CENTER><FORM><INPUT TYPE=\"IMAGE\" SRC=\"../images/annuler.png\" VALUE=\"Annuler\" OnClick=\"javascript:location='".$_SERVER ["PHP_SELF"]."'\"></FORM></CENTER>" ;
  }
 Echo "</BODY></HTML>" ;
?>